﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FarmerStore.ViewModels;
using FarmerStore.Models;
using System.Data.Objects;

namespace FarmerStore.Controllers
{
    public class StoreController : Controller
    {
        FarmerStoreEntities storeDB = new FarmerStoreEntities();

        //
        // GET: /Store/

        public ActionResult Index()
        {
            // Retrieve list of Genres from database
            var genres = from genre in storeDB.Genres
                         select genre;

            // Set up our ViewModel
            var viewModel = new StoreIndexViewModel()
            {
                Genres = genres.ToList(),
                NumberOfGenres = genres.Count()
            };

            // Return the view
            return View(viewModel);
        }

        //
        // GET: /Store/Browse?Genre=Disco

        public ActionResult Browse(int genreId,int parentId)
        {
            // Retrieve Genre and its Associated Products from database
            var genreModel = storeDB.Genres.Single<Genre>(item=>item.Id==genreId && item.ParentId==parentId);

            var childGenre=from item in storeDB.Genres
                           where item.ParentId==genreModel.Id
                           select item;

            var products = storeDB.GetProductsByGenreId(genreId);

            var viewModel = new StoreBrowseViewModel()
            {
                CurrentGenre = genreModel,
                Products = products.ToList(),
                ChildGenres = childGenre.ToList()
            };

            return View(viewModel);
        }

        public ActionResult Search(string genre)
        {
            // Retrieve Genre and its Associated Products from database
            var genreModel = storeDB.Genres.Single<Genre>(item => item.Name == genre);

            var childGenre = from item in storeDB.Genres
                             where item.ParentId == genreModel.Id
                             select item;

            var products = storeDB.GetProductsByGenreName(genre);

            var viewModel = new StoreBrowseViewModel()
            {
                CurrentGenre = genreModel,
                Products = products.ToList(),
                ChildGenres = childGenre.ToList()
            };

            return View(viewModel);
        }

        public ActionResult HotDeals()
        {
            // Retrieve list of Genres from database
            var genres = from genre in storeDB.Genres
                         select genre;

            // Set up our ViewModel
            var viewModel = new StoreIndexViewModel()
            {
                Genres = genres.ToList(),
                NumberOfGenres = genres.Count()
            };

            // Return the view
            return View(viewModel);
        }

        //
        // GET: /Store/Details/5

        public ActionResult Details(int id)
        {
            var product = storeDB.Products.Single(a => a.Id == id);

            return View(product);
        }

        //
        // GET: /Store/GenreMenu

        [ChildActionOnly]
        public ActionResult GenreMenu()
        {
            //var genres = storeDB.Genres.ToList();
            var genres = storeDB.GetGenres().ToList<GenreParentChildPairItem>();
            Dictionary<int,StoreGenreMenuViewModel> viewModel = new Dictionary<int,StoreGenreMenuViewModel>();
            StoreGenreMenuViewModel tempItem = null;
            foreach (var item in genres)
            {
                if (!viewModel.ContainsKey(item.GenreId))
                {
                    tempItem = new StoreGenreMenuViewModel { 
                        Parent = new Genre() {
                            Id=(int)item.GenreId
                            ,Name=item.Name
                            ,CreationDate=item.CreationDate
                            ,Description=item.Description
                            ,EditDate=item.EditDate
                            ,Path=item.Path
                            ,ParentId=item.ParentId
                            ,Remark=item.Remark}
                        , Childs =GetChilds(item.GenreId,genres)  };
                    viewModel.Add(item.GenreId,tempItem);                    
                }
            }
            return View(viewModel.Values.ToList <StoreGenreMenuViewModel>());
        }

        private List<Genre> GetChilds(int id,List<GenreParentChildPairItem> dataSource)
        {
            var result = from item in dataSource
                         where item.GenreId == id
                         select new Genre { Id=item.ChildId
                             ,Name=item.ChildName
                             ,Description=item.ChildDescription
                             ,CreationDate=item.ChildCreationDate
                             ,EditDate = item.ChildEditDate
                             ,Path = item.ChildPath
                             ,ParentId = item.GenreId
                             ,Remark = item.ChildRemark
                         };
            if (!result.IsNullOrEmpty<Genre>())
            {
                return result.ToList<Genre>();
            }
            else
            {
                return new List<Genre>();
            }

        }
    }
}
